import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import { ValidationPipe, VersioningType } from '@nestjs/common';
import { WINSTON_MODULE_NEST_PROVIDER } from 'nest-winston';
import { InvokeRecordInterceptor } from './middleware/format/invoke-record.interceptor';
import { FormatSuccessResponse } from './middleware/format/success-response';
import { FormatErrorFilter } from './middleware/format/error-response';
import { ConfigService } from '@nestjs/config';
async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  const configService = app.get(ConfigService);
  app.useGlobalPipes(new ValidationPipe());
  app.useLogger(app.get(WINSTON_MODULE_NEST_PROVIDER));
  app.useGlobalInterceptors(new InvokeRecordInterceptor());

  app.useGlobalInterceptors(new FormatSuccessResponse());
  app.useGlobalFilters(new FormatErrorFilter());

  const config = new DocumentBuilder()
    .setTitle('Nest-template')
    .setDescription('Nest.js prisma 后端项目模板')
    .setVersion('V1.0.0')
    .addBearerAuth({
      type: 'http',
      description: '基于 jwt 的认证',
    })
    .build();
  const document = SwaggerModule.createDocument(app, config);
  SwaggerModule.setup('swagger-doc', app, document);
  app.enableVersioning({
    type: VersioningType.URI,
    // type: VersioningType.HEADER,
    // header: 'version',
  });
  await app.listen(configService.get('nest_server_port'));
}
bootstrap();
